﻿using Api.Application.Command.UserRole;
using Domain;
using ErrorCode;
using Infrastructure;
using MediatR;

namespace Api.Application.Handler.UserRole
{
    public class DelUserRoleHandler : IRequestHandler<DelUserRoleCommand, ApiResult<int>>
    {
        private readonly IBase<UserRoleModel> userRoleRepo;

        public DelUserRoleHandler(IBase<UserRoleModel> userRoleRepo)
        {
            this.userRoleRepo = userRoleRepo;
        }
        public async Task<ApiResult<int>> Handle(DelUserRoleCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res=new ApiResult<int>();
            var info = userRoleRepo.GetAll().Where(x => x.UserId == request.UserId && x.RoleId == request.RoleId).FirstOrDefault();
            var user = await userRoleRepo.Del(info);

            if (user > 0)
            {
                res.Data = user;
                res.Code = ApiEnum.OK;
                res.Msg = "删除成功";
                return await Task.FromResult(res);
            }
            else
            {
                res.Data = user;
                res.Code = ApiEnum.Fail ;
                res.Msg = "删除失败";
                return await Task.FromResult(res);
            }

        }
    }
}
